/*
 * Copyright (c) 2002-2003 by OpenSymphony
 * All rights reserved.
 */
package com.company.workflow.loader;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.w3c.dom.Element;

import com.opensymphony.workflow.loader.XMLUtil;


/**
 * @author <a href="mailto:plightbo@hotmail.com">Pat Lightbody</a>
 * @version $Revision: 1.4 $
 */
public class ValidatorDescriptor extends AbstractDescriptor {
    //~ Instance fields ////////////////////////////////////////////////////////

    protected Map args = new HashMap();
    protected String type;

    //~ Constructors ///////////////////////////////////////////////////////////

    public ValidatorDescriptor() {
    }

    //~ Methods ////////////////////////////////////////////////////////////////

    public Map getArgs() {
        return args;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }


 

	public String getXML() {
		StringBuffer buf = new StringBuffer();
		
        buf.append("<validator " + (hasId() ? ("id=\"" + getId() + "\" ") : "") + "type=\"" + type + "\">");

        Iterator iter = args.entrySet().iterator();

        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            buf.append("<arg name=\"");
            buf.append(entry.getKey());
            buf.append("\">");

            if ("beanshell".equals(type) || "bsf".equals(type)) {
            	buf.append("<![CDATA[");
            	buf.append(entry.getValue());
            	buf.append("]]>");
            } else {
            	buf.append(XMLUtil.encode(entry.getValue()));
            }

            buf.append("</arg>");
        }

        buf.append("</validator>");
        
        return buf.toString();
	}
}
